package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.provider.service.BusScholartyrantService;
import com.meida.module.pay.client.entity.PayBalance;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.user.client.entity.AppSchool;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.handler.BindMobileAfterHandler;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AppUserBindMobileHandler implements BindMobileAfterHandler {

    @Autowired
    private BusScholartyrantService scholartyrantService;

    @Autowired
    private PayBalanceService payBalanceService;

    @Autowired
    private AppUserService appUserService;

    @Override
    public EntityMap bindSuccessAfter(Long userId) {
        EntityMap map = new EntityMap();
        Boolean isScholarTyrant = scholartyrantService.isScholarTyrant(userId);
        map.put("isScholarTyrant", isScholarTyrant);
        map.put("isSetPayPwd", false);
        PayBalance payBalance = payBalanceService.getByUserId(userId);
        if (FlymeUtils.isNotEmpty(payBalance)) {
            String paypwd = payBalance.getPaypwd();
            if (FlymeUtils.isNotEmpty(paypwd)) {
                map.put("isSetPayPwd", true);
            }
        }

        CriteriaQuery cq = new CriteriaQuery(AppUser.class);
        cq.eq(AppUser.class, "userId", userId);
        cq.select(AppUser.class, "userType");
        cq.select(AppSchool.class, "schoolName", "schoolAbridge");
        cq.createJoin(AppSchool.class).setMainField("schoolId").setJoinField("schoolId");
        EntityMap userInfo = appUserService.findOne(cq);
        map.put("schoolName", "");
        map.put("schoolAbridge", "");
        Boolean openXueBa = false;
        if (FlymeUtils.isNotEmpty(userInfo)) {
            String userType = userInfo.get("userType");
            if (userType.equals("SCHOLARTYRANT")) {
                openXueBa = true;
            }
            map.putAll(userInfo);
        }
        map.put("openXueBa", openXueBa);
        return map;
    }
}
